﻿using System;
using System.Web;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table.DataServices;
using Smarly.Core;

namespace Smarly.Logs
{
	public class LogEntry : TableServiceEntity
	{

		public LogEntry()
		{
			var now = DateTime.UtcNow;
			PartitionKey = string.Format("{0:yyyy-MM-dd}", now);
			RowKey = string.Format("{0:HH:mm:ss.ffffff}  {1}", now, Guid.NewGuid());
		}

		#region Table columns
		public string Message { get; set; }
		public string Level { get; set; }
		public string LoggerName { get; set; }
		public string Domain { get; set; }
		public string ThreadName { get; set; }
		public string Identity { get; set; }
		public string Exception { get; set; }
		public bool Viewed { get; set; }

		public string CreateDate { get; set; }


		#region UnknownPage

		public string UserName { get; set; }

		public string IpAddress { get; set; }

		public string BrowserString { get; set; }

		public string PageUrl { get; set; }

		public string QueryString { get; set; }

		public string ReferralUrl { get; set; }

		public string HttpMethod { get; set; }

		public string UserHostName { get; set; }
		public string FullUrl { get; set; }

		#endregion


//		public string RoleInstance { get; set; }
//		public string DeploymentId { get; set; }
		#endregion
	}

	public static class UnknownPageExceptionExt
	{
		public static void Populate(this UnknownPageException ex, LogEntry entry)
		{
			var request = ex.Request;
			var server = ex.Server;
			if (server == null || request == null)
				return;

			if (ex.User != null && ex.User.Identity.IsAuthenticated)
				entry.UserName = ex.User.Identity.Name;

			entry.IpAddress = request.ServerVariables["REMOTE_ADDR"];
			entry.BrowserString = request.UserAgent;

			if (request.UrlReferrer != null)
				entry.ReferralUrl = server.UrlDecode(request.UrlReferrer.ToString());

			entry.PageUrl = request.FilePath;
			entry.FullUrl = request.Url.ToString();
			entry.QueryString = request.QueryString.ToString();
			entry.HttpMethod = request.HttpMethod;

			try
			{
				entry.UserHostName = request.UserHostName;
			}
			catch{}
		}
		public static void Populate(this AspNetException ex, LogEntry entry)
		{
			var request = ex.Request;
			var server = ex.Server;
			if (server == null || request == null)
				return;

			if (ex.User != null && ex.User.Identity.IsAuthenticated)
				entry.UserName = ex.User.Identity.Name;

			entry.IpAddress = request.ServerVariables["REMOTE_ADDR"];
			entry.BrowserString = request.UserAgent;

//			if (request.UrlReferrer != null)
			entry.ReferralUrl = request.Headers["Referer"];//server.UrlDecode(request.UrlReferrer.ToString());

			entry.PageUrl = request.FilePath;
			entry.FullUrl = request.Url.ToString();
			entry.QueryString = request.QueryString.ToString();
			entry.HttpMethod = request.HttpMethod;

			try
			{
				entry.UserHostName = request.UserHostName;
			}
			catch{}
		}
	}
}